package com.stadium.handler;

import java.text.ParseException;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.stadium.bean.PlaceOrder;
import com.stadium.servlet.PlaceOrderServlet;
import com.stadium.tools.ResultMessage;

@Controller
@RequestMapping(value = "views")
public class PlaceOrderHandler {

	
	@Autowired
	private PlaceOrderServlet placeOrderServlet;

	private final Logger log = LoggerFactory.getLogger(PlaceHandler.class);
	
	
	
	/*
	 * 获取所有的场地租借信息
	 */
	@RequestMapping(value = "/findAll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAll(
			@RequestParam(value = "currentPage", defaultValue = "0") Integer currentPage,
			@RequestParam(value = "size", defaultValue = "5") Integer size,Integer type) {
		log.info("获取所有的场地租借信息");
		try {
			ResultMessage result = new ResultMessage();
			List<PlaceOrder> page = placeOrderServlet.findAll(type);
			result.getResultParm().put("placeOrder",page);
			result.setResultInfo("获取场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}

	/*
	 * 添加新的场地申请
	 */
	@RequestMapping(value = "/addPlace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> addPlace(PlaceOrder place) throws ParseException {
		log.info("添加新的场地");
		ResultMessage result = new ResultMessage();
	
			placeOrderServlet.addPlaceOrder(place);
			result.setResultInfo("添加新的场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		
	}

	/*
	 * 更新订单状态
	 */
	@RequestMapping(value = "/updataStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updataStatus(Integer placeId,Integer type) {
		log.info("更新订单状态");
		ResultMessage result = new ResultMessage();
		try {
			placeOrderServlet.setType(placeId,type);
			result.setResultInfo("更新订单状态");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("更新失败");
		}
	}

	/*
	 * 通过搜索名字获取所有的场地
	 */
	@RequestMapping(value = "/findAllByAccount", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAllByAccount() {
		log.info("获取所有的场地申请信息");
		try {
			// 获取session，并在session中获取account的值
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			String account = (String) session.getAttribute("account");
			ResultMessage result = new ResultMessage();
			result.getResultParm().put("places", placeOrderServlet.findAllByBorrowAccount(account));
			result.setResultInfo("获取场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}
	
	
	/*
	 * 删除场地操作
	 */
	@RequestMapping(value = "/deletePlace", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deletePlace(Integer placeId) {
		log.info("删除场地订单");
		ResultMessage result = new ResultMessage();
	
			placeOrderServlet.deletePlaceOrder(placeId);
			result.setResultInfo("删除场地订单成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		
	}
}
